import 'package:flutter/material.dart';
import 'package:stage_app/constants/color_constant.dart';
import 'package:stage_app/constants/constant.dart';
import 'package:stage_app/model/function_item.dart';
import 'package:stage_app/utils/util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class UserCenterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _UserCenterPageState();
}

class _UserCenterPageState extends State<UserCenterPage> {
  List<FunctionItem> _fuctionItems = [
    new FunctionItem(
        icon: 'information.png', title: '我的钱包', subTitle: '我的余额充值、提现、银行卡'),
    new FunctionItem(icon: 'data.png', title: '数据统计', subTitle: '实时获取当日数据掌握信息'),
    new FunctionItem(
        icon: 'information.png', title: '驿站信息', subTitle: '实时获取当日数据掌握信息'),
    new FunctionItem(icon: 'branch.png', title: '我的网点', subTitle: '驿站跟网点绑定关系'),
    new FunctionItem(
        icon: 'discount.png', title: '快递员', subTitle: '可设置快递员入库折扣优惠'),
    new FunctionItem(
        icon: 'service.png', title: '在线客服', subTitle: '08:00 ~ 18:00'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorConstant.app_bg,
      body: SafeArea(
        child: new Padding(
          padding: EdgeInsets.fromLTRB(SizeConstants.marginSize.w, 0, SizeConstants.marginSize.w, 0),
          child: CustomScrollView(
            physics: const BouncingScrollPhysics(),
            shrinkWrap: false,
            slivers: <Widget>[
              SliverToBoxAdapter(
                child: title(),
              ),
              SliverToBoxAdapter(
                child: userInfo(),
              ),
              SliverToBoxAdapter(
                child: userStatistics(),
              ),
              SliverToBoxAdapter(
                child: functionList(),
              ),
              SliverToBoxAdapter(
                child: btnLogout(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  title() {
    return new Container(
      height: 30.h,
      child: new Stack(
        children: <Widget>[
          new Center(
              child: Text(
            '个人中心',
            style: new TextStyle(fontSize: 17.sp, color: ColorConstant.text_dark),
          )),
          new Align(
            alignment: FractionalOffset.centerRight,
            child: Image.asset(
              Constant.ASSETS_IMG + 'user.jpg',
              height: 17.h,
              width: 17.w,
            ),
          )
        ],
      ),
    );
  }

  userInfo() {
    return new Padding(
      padding: EdgeInsets.only(top: 33.w),
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new GestureDetector(
            child: new Padding(
              padding: EdgeInsets.all(10.w),
              child: CircleAvatar(
                radius: 23.5.w,
                backgroundImage: AssetImage("assets/images/user.jpg"),
              ),
            ),
            onTap: () {
              handleAvatarClick();
            },
          ),
          new Padding(
            padding: EdgeInsets.all(0),
            child: Text(
              'Dashborad',
              style: new TextStyle(
                  fontSize: 18.sp,
                  color: ColorConstant.text_user_center_nick_name),
            ),
          )
        ],
      ),
    );
  }

  handleAvatarClick() {
    Util.showSnackBar(context, '点击了头像');
  }

  userStatistics() {
    return new Container(
        margin: EdgeInsets.symmetric(vertical: 12.5.w, horizontal: 17.5.w),
        height: 87,
        decoration: BoxDecoration(
            image: new DecorationImage(
                image: AssetImage(Constant.ASSETS_IMG + 'bg.png'),
                fit: BoxFit.cover)),
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Expanded(
              child: new Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.w),
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      '现金余额',
                      style: new TextStyle(
                          fontSize: 12.sp, color: ColorConstant.gray_ac),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.5.w),
                      child: Text(
                        '2400000000000000.00',
                        style: new TextStyle(fontSize: 18.sp, color: Colors.white),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              child: new Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.w),
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      '短信条数',
                      style: new TextStyle(
                          fontSize: 12.sp, color: ColorConstant.gray_ac),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.5.w),
                      child: Text(
                        '2400',
                        style: new TextStyle(fontSize: 18.sp, color: Colors.white),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ));
  }

  functionList() {
    return new Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: getFunctionItem(),
    );
  }

  btnLogout() {
    return new Container(
      margin: EdgeInsets.symmetric(vertical: 39.5.w),
      child: new GestureDetector(
        child: Center(
          child: Text(
            '退出登录',
            style: new TextStyle(
                fontSize: 14.sp, color: ColorConstant.text_user_center_logout),
          ),
        ),
        onTap: () {
          handleLogout();
        },
      ),
    );
  }

  void handleLogout() {}

  List<Widget> getFunctionItem() {
    List<Widget> children = new List();
    children.add(new Padding(
      padding: EdgeInsets.only(bottom: 9.5.w),
      child: Text('功能应用', style: new TextStyle(fontSize: 15.sp)),
    ));
    for (FunctionItem item in _fuctionItems) {
      Container child = new Container(
        margin: EdgeInsets.symmetric(vertical: 5.5.w),
        padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 24.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10), // 圆角
        ),
        child: new Row(
          children: <Widget>[
            CircleAvatar(
              radius: 16.5,
              backgroundColor: Colors.transparent,
              backgroundImage: AssetImage(Constant.ASSETS_IMG + item.icon),
            ),
            new Expanded(
                child: new Padding(
              padding: EdgeInsets.symmetric(horizontal: 11.5.w),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Text(
                    item.title,
                    style: new TextStyle(fontSize: 14.sp),
                  ),
                  new Text(
                    item.subTitle,
                    style: new TextStyle(
                        fontSize: 12.sp, color: ColorConstant.gray_cc),
                  )
                ],
              ),
            )),
            Image.asset(
              Constant.ASSETS_IMG + 'jump.png',
              width: 37.5.w,
              height: 37.5.h,
            ),
          ],
        ),
      );
      children.add(child);
    }
    return children;
  }
}
